﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using IBatisNet.DataMapper;

namespace StateGrid.NOTICE
{
    /// <summary>
    /// Summary description for NoticeDAO
    /// </summary>
    public class Notice_DAO
    {
        private ISqlMapper sqlMapper = Mapper.Instance();

        /// <summary>
        /// 获取所有的通知
        /// </summary>
        /// <returns></returns>
        public IList<Notice> SelectAllNOTICE()
        {
            return sqlMapper.QueryForList<Notice>("SelectAllNOTICE", null);
        }

        /// <summary>
        /// 获取指定ID的通知
        /// </summary>
        /// <param name="id">给定的ID</param>
        /// <returns></returns>
        public Notice SelectNOTICEByID(int id)
        {
            return sqlMapper.QueryForObject<Notice>("SelectNOTICEByID", id);
        }
        /// <summary>
        /// 统计通知总数
        /// </summary>
        /// <returns></returns>
        public int CountNOTICE()
        {
            return sqlMapper.QueryForObject<int>("CountNOTICE",null);
        }

        /// <summary>
        /// 获取指定区间的通知
        /// </summary>
        /// <param name="start">开始位置</param>
        /// <param name="end">结束位置</param>
        /// <returns></returns>
        public IList<Notice> SelectSomeNOTICE(int start, int end)
        {
            IDictionary<String, Object> para = new Dictionary<String, Object>();
            para.Add("start", start);
            para.Add("end", end);
            return sqlMapper.QueryForList<Notice>("SelectSomeNOTICE", para);
        }

        /// <summary>
        /// 插入一条新通知
        /// </summary>
        /// <param name="notice">通知</param>
        /// <returns></returns>
        public Boolean InsertNOTICE(Notice notice)
        {
            Boolean flag = true;
            try
            {
                sqlMapper.Insert("InsertNOTICE", notice);
            }
            catch (Exception)
            {
                flag = false;
            }
            return flag;
        }

        /// <summary>
        /// 更新通知，更新的字段有标题、内容
        /// </summary>
        /// <param name="notice">要更新的通知</param>
        /// <returns></returns>
        public Boolean UpdateNOTICE(Notice notice)
        {
            Boolean flag = true;
            int rt=sqlMapper.Update("UpdateNOTICE", notice);
            if (rt == 0)
            {
                flag = false;
            }
            return flag;
        }

        /// <summary>
        /// 更新通知访问次数
        /// </summary>
        /// <param name="notice">要更新的通知</param>
        /// <returns></returns>
        public Boolean UpdateNOTICE_VISIT_COUNT(Notice notice)
        {
            Boolean flag = true;
            int rt = sqlMapper.Update("UpdateNOTICE_VISIT_COUNT", notice);
            if (rt == 0)
            {
                flag = false;
            }
            return flag;
        }

        /// <summary>
        /// 删除指定ID的通知
        /// </summary>
        /// <param name="id">要删除通知的ID</param>
        /// <returns></returns>
        public Boolean DeleteNOTICEByID(int id)
        {
            Boolean flag = true;
            int rt = sqlMapper.Delete("DeleteNOTICEByID", id);
            if (rt == 0)
            {
                flag = false;
            }
            return flag;
        }
        public Notice_DAO()
        {
            //
            // TODO: Add constructor logic here
            //
        }
    }
}